package com.base.component.ai.workflow.flow;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.component.ai.workflow.core.FieldTypeEnum;
import com.base.component.ai.workflow.core.NodeTypeEnum;
import com.base.component.ai.workflow.node.Node;
import com.base.component.ai.workflow.node.NodeAgent;
import com.base.component.ai.workflow.node.NodeCode;
import com.base.component.ai.workflow.node.NodeCondition;
import com.base.component.ai.workflow.node.NodeDify;
import com.base.component.ai.workflow.node.NodeEnd;
import com.base.component.ai.workflow.node.NodeFunction;
import com.base.component.ai.workflow.node.NodeHttp;
import com.base.component.ai.workflow.node.NodeInject;
import com.base.component.ai.workflow.node.NodeJdbcSelect;
import com.base.component.ai.workflow.node.NodeLLM;
import com.base.component.ai.workflow.node.NodeVariable;
import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.core.json.JsonArray;
import com.gitee.magic.core.json.JsonObject;

public class FlowFactory {

	public static Flow getFlow(JsonObject json) {
		Flow flow=Flow.ofFlow(json.getString("name"), getNode(json.getJsonObject("node")));
		if(json.has("nexts")) {
			JsonArray nextsArray=json.getJsonArray("nexts");
			List<Flow> nextFlows=new ArrayList<>();
			for(int i=0;i<nextsArray.length();i++) {
				JsonObject nextNode=nextsArray.getJsonObject(i);
				nextFlows.add(getFlow(nextNode));
			}
			flow.setNexts(nextFlows);
		}
		
		return flow;
	}
	
	public static Node getNode(JsonObject json) {
		NodeTypeEnum nodeType=json.getEnum(NodeTypeEnum.class,"nodeType");
		if(NodeTypeEnum.CODE==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeCode.class, json);
		}else if(NodeTypeEnum.AGENT==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeAgent.class, json);
		}else if(NodeTypeEnum.CONDITION==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeCondition.class, json);
		}else if(NodeTypeEnum.DIFY==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeDify.class, json);
		}else if(NodeTypeEnum.END==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeEnd.class, json);
		}else if(NodeTypeEnum.FUNCTION==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeFunction.class, json);
		}else if(NodeTypeEnum.HTTP==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeHttp.class, json);
		}else if(NodeTypeEnum.INJECT==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeInject.class, json);
		}else if(NodeTypeEnum.JDBC_SELECT==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeJdbcSelect.class, json);
		}else if(NodeTypeEnum.LLM==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeLLM.class, json);
		}else if(NodeTypeEnum.VARIABLE==nodeType) {
			return ConverterEditorUtils.restoreObject(NodeVariable.class, json);
		}
		return null;
	}
	
	public static void main(String[] args) {
		
		NodeJdbcSelect njd=new NodeJdbcSelect();
		njd.setParameterNames(Arrays.asList("userId"));
		njd.setVariableName("order");
		njd.setSqlId("order_id");
		njd.setValueType(FieldTypeEnum.String);
		Flow flowNjd=Flow.ofFlow("读取DB",njd);
		
		NodeAgent agent=new NodeAgent();
		agent.setBeanName("coreReply");
		agent.setContextParamInjects(Arrays.asList("name"));
		agent.setPromptParamInjects(Arrays.asList("name"));
		Flow flowAgent=Flow.ofFlow("Agent",agent);
		
		NodeEnd end=new NodeEnd();
		Map<String,String> endParams=new HashMap<>();
		endParams.put("type", "text");
		endParams.put("value", "值");
		end.setParams(endParams);
		Flow flowEnd=Flow.ofFlow("end",end);
		
		NodeCondition nc=new NodeCondition();
		Map<String,List<Flow>> expression=new HashMap<>();
		expression.put("flowAgentName", Arrays.asList(flowEnd));
		nc.setExpression(expression);
		Flow flowNc=Flow.ofFlow("nc",nc);
		
		NodeEnd end1=new NodeEnd();
		end1.setParams(new HashMap<>());
		Flow flowEnd1=Flow.ofFlow("end1",end1);
		
		flowNjd.setNexts(Arrays.asList(flowNc));
		flowNc.setNexts(Arrays.asList(flowAgent));
		flowAgent.setNexts(Arrays.asList(flowEnd1));
		
		System.out.println(ConverterEditorUtils.converterObject(flowNjd));
	}
	
}
